package com.webull.information.center.validator;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.webull.information.center.common.constants.CenterCode;
import com.webull.information.center.common.util.CenterExceptionUtil;
import com.webull.information.client.model.News;
import com.webull.information.client.model.parm.NewsParam;

/**
 * 新闻验证器，主要对客户端提交的参数进行验证
 *
 * @author chengang
 * @version 1.0
 * @date 16-5-20
 * @time 下午4:35
 * @since JDK 1.8
 */
@Repository
public class NewsValidator extends BaseValidator {

	public void validateSearchByTickerType(Integer[] regionIds, Integer tickerType, Long currentNewsId, Integer pageSize, String lang) {
		if (regionIds != null) {
			validateSearchByIds(regionIds);
		}
		validateLangIsError(lang, CenterCode.PARAM_ERROR);
		validateIntegerTypeIsNull(tickerType, CenterCode.PARAM_NULL, "tickerType");
		validateIntegerTypeIsError(tickerType, CenterCode.PARAM_ERROR, "tickerType");
		// if (currentNewsId != null) {
		// validateLongTypeIsError(currentNewsId, CenterCode.PARAM_ERROR, "currentNewsId");
		// }
		validateIntegerTypeIsNull(pageSize, CenterCode.PARAM_NULL, "pageSize");
		validateBusTypeOverLimit(pageSize, 0, 100, CenterCode.PARAM_ERROR, "pageSize");
	}

	public void validateRegionIdsError(String regionIds, Integer pageSize) {
		validatorIdsWithComma(regionIds);
		if (pageSize == null || pageSize <= 0) {
			log.error("整型类型错误,regionIds={},pageSize={},", regionIds, pageSize);
			throw CenterExceptionUtil.createCenterException(CenterCode.PARAM_ERROR, "regionIds is empty");
		}

	}

	public void validateStringError(String str) {
		if (StringUtils.isBlank(str)) {
			log.error("validateStringError,str={} ", str);
			throw CenterExceptionUtil.createCenterException(CenterCode.PARAM_ERROR, "parm is empty");
		}
	}

	public void validateObjectNull(Object object) {
		if (object == null) {
			throw CenterExceptionUtil.createCenterException(CenterCode.PARAM_ERROR, "parm is empty");
		}
	}

	public void validateNewsParam(NewsParam newsParam) {
		if (newsParam == null || newsParam.getPageIndex() == null || newsParam.getPageIndex() <= 0 || newsParam.getPageSize() == null
				|| newsParam.getPageSize() <= 0) {
			throw CenterExceptionUtil.createCenterException(CenterCode.PARAM_ERROR, "newsParam is empty");
		}
	}

	public void validateIdsList(List<?> ids) {
		if (ids == null || ids.isEmpty()) {
			throw CenterExceptionUtil.createCenterException(CenterCode.PARAM_ERROR, "ids is empty");
		}
	}

	public void validateUpdate(News news) {
		validateObjectIsNull(news, CenterCode.PARAM_NULL);
		validateGetByLongId(news.getId());

		if (StringUtils.isNotBlank(news.getTitle())) {
			validateStrTypeIsError(news.getTitle(), CenterCode.PARAM_ERROR, "title");
			validateStrTypeOverLimit(news.getTitle(), 300, CenterCode.PARAM_ERROR, "title");
		}

		if (news.getSummary() != null) {
			validateStrTypeIsError(news.getSummary(), CenterCode.PARAM_ERROR, "Summary");
			validateStrTypeOverLimit(news.getSummary(), 300, CenterCode.PARAM_ERROR, "Summary");
		}

		if (news.getMainPic() != null) {
			validateStrTypeIsError(news.getMainPic(), CenterCode.PARAM_ERROR, "MainPic");
			validateStrTypeOverLimit(news.getMainPic(), 100, CenterCode.PARAM_ERROR, "MainPic");
		}

		if (news.getSourceName() != null) {
			validateStrTypeIsError(news.getSourceName(), CenterCode.PARAM_ERROR, "SourceName");
			validateStrTypeOverLimit(news.getSourceName(), 100, CenterCode.PARAM_ERROR, "SourceName");
		}

		if (news.getUrl() != null) {
			validateStrTypeIsError(news.getUrl(), CenterCode.PARAM_ERROR, "Url");
			validateStrTypeOverLimit(news.getUrl(), 100, CenterCode.PARAM_ERROR, "Url");
		}
		if (news.getUrl() != null) {
			validateStrTypeIsError(news.getUrl(), CenterCode.PARAM_ERROR, "Url");
			validateStrTypeOverLimit(news.getUrl(), 100, CenterCode.PARAM_ERROR, "Url");
		}

		if (news.getRegionId() != null) {
			validateIntegerTypeIsError(news.getRegionId(), CenterCode.PARAM_ERROR, "RegionId");
		}

		if (news.getLanguage() != null) {
			validateStrTypeIsError(news.getLanguage(), CenterCode.PARAM_ERROR, "Language");
			validateStrTypeOverLimit(news.getLanguage(), 20, CenterCode.PARAM_ERROR, "Language");
		}

	}

}
